<template>
  <div class="mod-schedule"  style="padding: 20px 10px;">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.beanName" placeholder="sql名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="interfaceName"
        header-align="center"
        align="center"
        label="接口名称">
      </el-table-column>
      <el-table-column
        prop="sqlStr"
        header-align="center"
        align="center"
        width="800"
        label="sql语句">
      </el-table-column>
      <el-table-column
        prop="note"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-popconfirm title="确定删除吗？" icon-color="#f00" @confirm="sendRquestDelete([scope.row.id])">
              <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
          <el-button  @click="verification(scope.row)" type="text" size="small">验证</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right;padding-top: 10px;">
      <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <el-dialog
      width="45%"
      :title="titleStr"
      :visible.sync="editPostJsonFlag"
      :close-on-click-modal="false"
      append-to-body>
      <div v-if="editPostJsonFlag">
        <vue-json-editor
          v-model="resultInfo"
          :showBtns="false"
          :mode="'code'"
          @json-change="onJsonChange"
          @json-save="onJsonSave"
          @has-error="onError"
        />
        <br>
      <el-button :disabled="showloading" :type="!showloading?'primary':'success'" @click="checkJson">
        {{ !showloading?'确定':'等待……' }}
      </el-button>
      <div v-if="resultInfo2!=''" style="max-height:250px;overflow-y: scroll;border:2px solid #eee;margin:10px 0 0 0 ">
        <pre> {{ resultInfo2 }}</pre>
      </div>
    </div>
    </el-dialog>
  </div>
</template>

<script>
   import vueJsonEditor from 'vue-json-editor'
  import AddOrUpdate from './schedule-add-or-update'
  import request from '@/utils/request'
  export default {
    data () {
      return {
        showloading:false,
        titleStr:"JSON参数验证",
        resultInfo:{},
        resultInfo2:"",
        editPostJsonFlag:false,
        queryStr:"",
        dataForm: {
          beanName: ''
        },
        hasJsonFlag:true,  // json是否验证通过
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        logVisible: false
      }
    },
    components: {
      AddOrUpdate,
      vueJsonEditor
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 检查json
      checkJson(){
        this.showloading = true
        request({
            url: '/ht-datadevelop/execute/asyncRequestApi/'+this.queryStr,
            method: 'post',
            data: this.resultInfo
          }).then((res) => {
            this.showloading = false
            if(res.code==200){
              this.resultInfo2 = res.data.dataList||'无数据'
            }
          })
      },
      onJsonChange (value) {
        this.onJsonSave(value)
      },
      onJsonSave (value) {
        this.resultInfo = value
        this.hasJsonFlag = true
      },
      onError(value) {
        this.hasJsonFlag = false
      },
      verification(v){
          this.showloading = false
          this.resultInfo = {}
          this.queryStr = v.interfaceName
          this.resultInfo2 =  ''
          this.editPostJsonFlag = true
        },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        request({
          url: '/ht-datadevelop/execute/list',
          method: 'post',
          data:{
            name:this.dataForm.beanName,
            page:this.pageIndex+"",
            limit:this.pageSize+""
          }
        }).then((res) => {
          if (res.code === 200) {
            this.dataList = res.data.list
            this.totalPage = res.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },

      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row)
        })
      },
      sendRquestDelete(ids){
        request({
            url: '/ht-datadevelop/execute/deleteByIds',
            method: 'post',
            data:{ids:ids}
          }).then((res) => {
            if (res.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          })
        },
      // 删除
      deleteHandle (id) {
        var ids = this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定批量删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.sendRquestDelete(ids)
        }).catch(() => {})
      },
    }
  }
</script>
